/**
 * @UserData封装本地存储
 * 支持  IE5,6,7,8+
 * 		支持同一级别目录下的页面数据共享，不同目录下一个页面对应一个xml存储单元
 * 		win7位于C:\Users\用户名\AppData\Roaming\Microsoft\Internet Explorer\UserData
 * 		userData的保存形式为XML文件 单个文件大小限制为128KB,一个域名下文件大小限制为1024KB 根据浏览器不同安全级别依次缩小 最好控制在64KB下
 * @author likang
 * @version 1.0
 */
var UserData = {
    userData: null,
    name: window.location.hostname,    
    init: function(){
        if (!this.userData) {
            try {
                this.userData = document.createElement('input');
				this.userData.style.display = "none";
                this.userData.type = "hidden";
                this.userData.addBehavior("#default#userData");
                document.body.appendChild(this.userData);
                var expires = new Date();
				//一年过期时间
                expires.setDate(expires.getDate() + 365);
                this.userData.expires = expires.toUTCString();
            } 
            catch (e) {
                return false;
            }
        }
        return true;
    },
    
    setItem: function(key, value){
        if (this.init() && value != null) {
            this.userData.load(this.name);
            this.userData.setAttribute(key, value);
            this.userData.save(this.name);
        }
    },
    
    getItem: function(key){
        if (this.init()) {
            this.userData.load(this.name);
            return this.userData.getAttribute(key)
        }
    },
    
    removeItem: function(key){
        if (this.init()) {
            this.userData.load(this.name);
            this.userData.removeAttribute(key);
            this.userData.save(this.name);
        }
    }
};


/**
 * @localStorage封装本地存储 浏览器一般支持5MB存储空间
 * 支持  IE8.0+
 * 		FF 3.0+
 * 		Opera 10.5+
 *		Chrome 4.0+
 * 		Safari 4.0+
 * 		Iphone 2.0+
 * 		Android 2.0+
 * 		在IE8.0下不支持跨页面的本地存储
 * @author likang
 * @version 1.0
 */
var LocalStorage = {
	 isSupportFlag : null,
    /**
     * 保存属性
     * @param {String} name 属性名
     * @param {String} value 属性值
     */
    setItem : function(name,value){
        if(this.isSupport() && value != null){
            localStorage.setItem(name,value);
        }
    },
	/**
     * 获取属性值
     * @param {String} name 属性名
     * @return {String}
     */
	 getItem : function(name){
	  	var val = null;
        if(this.isSupport()){
             val = localStorage.getItem(name);
        }
        return val;
    },
    /**
     * 删除属性
     * @param {String} name 属性名
     */
    removeItem : function(name){
        if (this.isSupport()) {
			localStorage.removeItem(name);
		}
    },
	 /**
     * 删除所有属性
     */
    removeAll : function(){
        if (this.isSupport()) {
			localStorage.clear();
		}
    },
    /**
     * 判断浏览器是否支持localStorage本地存储
     */
    isSupport : function(){
        var isf = this.isSupportFlag;
        if(isf == null){
            if(window.localStorage){
                this.isSupportFlag = true;
            } else {
				this.isSupportFlag = false;
			}
        }
        return this.isSupportFlag;
    }
};

/**
 * @localStore封装本地存储 暴露统一调用接口
 * 支持ie,ff,sf等所有浏览器
 * @author likang
 * @version 1.0
 */
var localStore = {
	isSupportLocalStorageFlag : null,
  	isSupportLocalStorage: function(){
	  	if(this.isSupportLocalStorageFlag == null){
			this.isSupportLocalStorageFlag = LocalStorage.isSupport();
		}
	  	return this.isSupportLocalStorageFlag;
	 },
	 setItem : function(name,value){
        if(this.isSupportLocalStorage()&& value != null && name != null){
            localStorage.setItem(name,value);
        } else {
			UserData.setItem(name,value);
		}
    },
	/**
     * 获取属性值
     * @param {String} name 属性名
     * @return {String}
     */
	 getItem : function(name){
	  	var val = null;
        if(this.isSupportLocalStorage()&& name != null){
            val = localStorage.getItem(name);
        } else {
			val = UserData.getItem(name);
		}
        return val;
    },
    /**
     * 删除属性
     * @param {String} name 属性名
     */
    removeItem : function(name){
        if(this.isSupportLocalStorage()&& name != null){
            localStorage.removeItem(name);
        } else {
			UserData.removeItem(name);
		}
    },
	removeAll : function(){
        if(this.isSupportLocalStorage()){
            localStorage.removeAll();
        } 
    }
};

